package cn.edu.xust.service.impl;

import cn.edu.xust.Ro.MulHotelEleRo;
import cn.edu.xust.Ro.MulHotelWtRo;
import cn.edu.xust.Ro.PieData;
import cn.edu.xust.entity.WtdataEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;

import cn.edu.xust.dao.ElecdataDao;
import cn.edu.xust.entity.ElecdataEntity;
import cn.edu.xust.service.ElecdataService;


@Service("elecdataService")
public class ElecdataServiceImpl extends ServiceImpl<ElecdataDao, ElecdataEntity> implements ElecdataService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ElecdataEntity> page = this.page(
                new Query<ElecdataEntity>().getPage(params),
                new QueryWrapper<ElecdataEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ElecdataEntity> getByDeviceNameAndTimetag(List<String> deivcenamelist, String timetag) {
        QueryWrapper<ElecdataEntity> wap = new QueryWrapper<ElecdataEntity>().in("devicename", deivcenamelist).eq("timetypeflag", timetag);
        return baseMapper.selectList(wap);

    }


    /**
     * 向上查询6小时内的最新数据  openapi用这个
     *
     * @param date       2024-11-22
     * @param hour       6 那就是22.30-04.30    12 就是 19.30-10.30
     * @param devicename 00002024034154
     * @return WtdataEntity
     */
    @Override
    public ElecdataEntity searchNewstInHourtime(String date, int hour, String devicename) {

        date = date + " 10:30:00";

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
        LocalDateTime dateTime2 = LocalDateTime.parse(date, formatter);

        dateTime = dateTime.minusHours(12);
        String hourdate = dateTime.format(formatter);
        String hourdate2 = dateTime2.format(formatter);
        QueryWrapper<ElecdataEntity> wap = new QueryWrapper<ElecdataEntity>().gt("ctime", hourdate).lt("ctime", hourdate2);
        wap.eq("devicename", devicename);
        List<ElecdataEntity> list = baseMapper.selectList(wap);
        if (list.isEmpty()) {
            return null;
        }
        //拿最后一个 也就是maxid
        return list.get(list.size() - 1);
    }

    @Override
    public List<MulHotelEleRo> getByHType(String hotelid, String type, String dateStr) {
        //拿到前一日
        LocalDate date = LocalDate.parse(dateStr);
        LocalDate previousDate = date.minusDays(1);
        String previousDateStr = previousDate.toString();
        return baseMapper.getByHType(hotelid,type,previousDateStr+" 22:30:00",date+" 10:30:00");

    }

    @Override
    public List<PieData> getByDay(String name, String start, String end) {

        return baseMapper.getByDay( name,  start,  end);


    }
}